package com.wyj.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.wyj.pojo.GameUser;
import com.wyj.protoBuf.protoJava.GameUserAndToken;
import com.wyj.service.impl.UserServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpRequest;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

/**
 * @author ：wyj
 * @description：TODO
 * @date ：2022/12/9 2:26 下午
 */
@Controller
@RequestMapping("/user")
public class UserController {

    @Autowired
    UserServiceImpl userServiceImpl;

    @RequestMapping("/userIndex")
    public String getUserIndex() {
        return "hello wyj";
    }

    //获取登录用户的信息 将用户的信息跟token用fastJson封装返回
//    @GetMapping(value = "/getUserInfo")
//    @ResponseBody
//    public String getUserInfo(HttpServletRequest request) {
//        String token = request.getHeader("token");
//        GameUser gameUserByUUID = userServiceImpl.getGameUserByUUID(token);
//
//        List<Object> list = new ArrayList<>();
//        list.add(token);
//        list.add(gameUserByUUID);
//
//        String result = JSON.toJSONString(list, SerializerFeature.PrettyFormat);
//
//        //用户登录成功 将这个token+用户信息返回给前端
//        return result;
//    }


    //获取登录用户的信息 将用户的信息跟token用proto封装返回
    @GetMapping(value = "/getUserInfo")
    @ResponseBody
    public String getUserInfo(HttpServletRequest request) {
        String token = request.getHeader("token");
        GameUser gameUserByUUID = userServiceImpl.getGameUserByUUID(token);

        GameUserAndToken.GameUser result = GameUserAndToken.GameUser.newBuilder()
                .setId(gameUserByUUID.getId())
                .setGoldCoin(gameUserByUUID.getGoldCoin())
                .setUserLevel(gameUserByUUID.getUserLevel())
                .setUserName(gameUserByUUID.getUserName())
                .setUUID(gameUserByUUID.getUUID())
                .setVipLevel(gameUserByUUID.getVipLevel())
                .setToken(token)
                .build();


        return result.toString();
    }


}
